<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


if (!is_registered()) exit;

$id = intval ($_POST['id']);
$bad = false;
$good = false;
if ($_POST['value'] == 0) $bad = true;
if ($_POST['value'] == 1) $good = true;

$get_art = $db->query ("SELECT art_rating, art_voters FROM ".DB_PEREFIX."_articles WHERE art_id = '$id'");

if ($db->num_rows ($get_art) == 1) {
	$art = $db->fetch_array ($get_art);
	
	$vote = false;
	$voters = explode (',', $art['art_voters']);
	foreach ($voters as $v) if ($GLOBAL_USER['user_id'] == trim($v)) $vote = true;
	
	if ($art['art_voters'] == '')
		$new_voters = $GLOBAL_USER['user_id'];
	else
		$new_voters = $art['art_voters'].','.$GLOBAL_USER['user_id'];
	
	$rating = $art['art_rating'];
	if ($vote == false) {
		if ($bad == true) $rating--;
		if ($good == true) $rating++;
		$db->query ("UPDATE ".DB_PEREFIX."_articles SET art_rating = '$rating', art_voters = '$new_voters' WHERE art_id = '$id'");
	}
	($rating == 0)
		? $rating = '0 баллов'
		: $rating = count_smth ($rating, array ('балл', 'балла', 'баллов'));
	print $rating;
}

